<?php
// module/Album/src/Album/Model/AlbumTable.php
namespace Album\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;

class AlbumTable extends AbstractTableGateway
{
    protected $table = 'album';
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Album());
        
        $this->initialize();
    }
    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }
    public function getAlbum($id)
    {
        $id = (int)$id;
        $rowset = $this->select(array('id'=>$id));
        $row = $rowset->current();
        if(!$row){
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }
    public function saveAlbum(Album $album)
    {
        $data = array(
            'artist' => $album->artist,
            'title'  => $album->title
        );
        $id = (int)$album->id;
        if($id == 0){
            $this->insert($data);
        }elseif($this->getAlbum($id)){
            $this->update($data, array('id'=>$id));
        }else{
            throw new \Exception("Form Id not exist");
        }
    }
    public function deleteAlbum($id)
    {
        $this->delete(array('id'=>$id));
    }
}

?>
